package com.ljb.android.component.project.view.fragment

import android.graphics.Typeface
import android.text.TextUtils
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.android.material.tabs.TabLayout
import com.gyf.immersionbar.ImmersionBar
import com.ljb.android.comm.mvp.CommMvpFragment
import com.ljb.android.comm.router.core.RouterManager
import com.ljb.android.component.project.R
import com.ljb.android.component.project.adapter.ProjectTabAdapter
import com.ljb.android.component.project.bean.ProjectTabBean
import com.ljb.android.component.project.contract.ProjectMainContract
import com.ljb.android.component.project.databinding.ProjectFragmentMainBinding
import com.ljb.android.component.project.presenter.ProjectMainPresenter
import com.ljb.android.component.project.router.ProjectRouter
import com.ljb.android.component.search.router.SearchRouter

/**
 * @Author Kotlin MVP Plugin
 * @Date 2021/02/26
 * @Description input description
 **/
@Route(path = ProjectRouter.Fragment.PROJECT_MAIN)
class ProjectMainFragment :
    CommMvpFragment<ProjectMainContract.IPresenter, ProjectFragmentMainBinding>(),
    ProjectMainContract.IView {

    private var mTabViewPagerAdapter: ProjectTabAdapter? = null

    override fun registerPresenter() = ProjectMainPresenter::class.java

    override fun getLayoutId() = R.layout.project_fragment_main

    override fun registerBinding(
        inflater: LayoutInflater,
        parent: ViewGroup
    ): ProjectFragmentMainBinding {
        return ProjectFragmentMainBinding.inflate(inflater, parent, false)
    }

    override fun initStatusBar() {
        ImmersionBar.with(this)
            .transparentStatusBar()     //透明状态栏，不写默认透明色
            .statusBarDarkFont(false) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(false) //导航栏图标是深色，不写默认为亮色
            .init()
    }

    override fun supportTitle() = true

    override fun initView() {
        initTitleView()
    }

    override fun lazyInitData() {
        getPresenter().getTabList()
    }

    private fun initTitleView() {
        mTitleView?.setBackgroundResource(R.drawable.comm_shape_red_gradient)
        mTitleView?.setOnClickListener { scrollToTop() }
        setTitleText(R.string.project_main, resources.getColor(R.color.color_white))
        setTitleLeftImage(R.mipmap.comm_icon_home_left_menu) {
            openOrCloseDrawerLeft()
        }
        setTitleRightImage(R.mipmap.comm_icon_search) {
            goSearch()
        }
    }

    private fun openOrCloseDrawerLeft() {
        RouterManager.getAppService()?.openOrCloseDrawerLeft()
    }

    override fun onTabListSuccess(data: ProjectTabBean) {
        mBind.viewPage.run {
            offscreenPageLimit = 1
            mTabViewPagerAdapter = ProjectTabAdapter(data.data, childFragmentManager)
            adapter = mTabViewPagerAdapter
        }

        mBind.tabLayout.run {
            addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {

                private fun getTabCustomView(tab: TabLayout.Tab): TextView {
                    return TextView(activity).apply {
                        setLines(1)
                        ellipsize = TextUtils.TruncateAt.END
                        setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
                        setTextColor(resources.getColor(R.color.color_red))
                        typeface = Typeface.DEFAULT_BOLD
                        text = tab.text
                    }
                }

                override fun onTabSelected(tab: TabLayout.Tab) {
                    tab.customView = getTabCustomView(tab)
                }

                override fun onTabUnselected(tab: TabLayout.Tab) {
                    tab.customView = null
                }

                override fun onTabReselected(tab: TabLayout.Tab) {
                    if (tab.customView == null) {
                        tab.customView = getTabCustomView(tab)
                    }
                }
            })
            setupWithViewPager(mBind.viewPage)
            selectTab(getTabAt(0))
        }
    }

    private fun scrollToTop() {
        mTabViewPagerAdapter?.getCurFragment()?.scrollToTop()
    }

    private fun goSearch() {
        RouterManager.goActivity(SearchRouter.Activity.SEARCH_MAIN)
    }
}
